package com.hongyuan.venue.ui.main.view_mode;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.hongyuan.venue.base.CustomViewModel;
import com.hongyuan.venue.data.Constants;
import com.hongyuan.venue.data.MyRepository;
import com.hongyuan.venue.data.userbean.MemberLoginBean;
import com.hongyuan.venue.ui.main.activity.MainActivity;
import com.hongyuan.venue.ui.main.activity.RetrievePasswordActivity;
import com.hongyuan.venue.ui.main.activity.WebActivity;
import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.mvvmhabitx.bus.event.SingleLiveEvent;
import com.hongyuan.mvvmhabitx.utils.ToastUtils;

public class LoginPassWordViewModel extends CustomViewModel<MyRepository> {

    //用户名的绑定
    public ObservableField<String> userName = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");
    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //密码开关观察者
        public SingleLiveEvent<Boolean> pSwitchEvent = new SingleLiveEvent<>();
    }

    public LoginPassWordViewModel(@NonNull Application application, MyRepository model) {
        super(application, model);
    }

    //密码显示开关  (你可以尝试着狂按这个按钮,会发现它有防多次点击的功能)
    public BindingCommand passwordShowSwitchOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            //让观察者的数据改变,逻辑从ViewModel层转到View层，在View层的监听则会被调用
            uc.pSwitchEvent.setValue(uc.pSwitchEvent.getValue() == null || !uc.pSwitchEvent.getValue());
        }
    });

    //返回验证码登录
    public BindingCommand goCodeLogin = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            finish();
        }
    });
    //设置新密码
    public BindingCommand goGetNewPassword = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(RetrievePasswordActivity.class);
        }
    });
    //登录按钮的点击事件
    public BindingCommand loginOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            login();
        }
    });

    //用户协议
    public BindingCommand goXieYi = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("url","http://www.hongyuangood.com/xy/xy.html");
            bundle.putString("title","用户协议");
            startActivity(WebActivity.class,bundle);
        }
    });

    //隐私政策
    public BindingCommand goZhengCe = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putString("url","http://www.hongyuangood.com/xy/xy.html");
            bundle.putString("title","隐私政策");
            startActivity(WebActivity.class,bundle);
        }
    });


    /*
    * 登录请求
    * */
    private void login(){
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }

        clearParams().setParams("m_mobile", userName.get()).setParams("m_pwd",password.get());

        //登录请求
        requestData(Constants.MEMBERLOGIN);
    }

    @Override
    protected void returnData(int code, Object dataBean) {
        super.returnData(code, dataBean);

        if(code == Constants.MEMBERLOGIN){
            MemberLoginBean bean = (MemberLoginBean) dataBean;

            userToken.setM_id(bean.getM_id());
            userToken.setM_mobile(bean.getM_mobile());
            userToken.setRole_id(String.valueOf(bean.getRole_id()));

            //保存用户登录信息
            model.saveUser(bean);

            //极光推送设置别名或标签
            //JPushInterface.setAlias(getApplication().getApplicationContext(), 100, userToken.getM_mobile());

            startActivity(MainActivity.class);
            finish();
        }
    }
}
